package com.wyh.controller.gfive;

import com.wyh.common.result.Result;
import com.wyh.domain.dto.gfive.RefundPicDTO;
import com.wyh.domain.po.gfive.PageHelper;
import com.wyh.service.gfive.RefundService;
import io.swagger.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/refund")
@Api(tags = "财务管理-退款管理")
public class RefundController {

    @Resource
    private RefundService refundService;

    @GetMapping("/all")
    @ApiOperation("待退款全查询")
    public   Result<Map<String, Object>>getAllRefund(@RequestParam(required = false) @ApiParam("页数") Integer currentPage,
                                                     @RequestParam(required = false) @ApiParam("条数") Integer pageSize) {
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);

        return refundService.searchRefund(null,null,null,null,pageHelper);

    }

    @GetMapping("/search")
    @ApiOperation("待退款条件查询")
    public   Result<Map<String, Object>> searchRefund(
            @RequestParam(required = false) @ApiParam(value = "客户姓名", example = "重庆蓝精灵科技公司") String customerName,
            @RequestParam(required = false) @ApiParam(value = "客户电话", example = "13972837483") String customerPhone,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @ApiParam(value = "开始日期", example = "2024-05-20") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @ApiParam(value = "结束日期", example = "2024-08-25") LocalDate endDate,
            @RequestParam(required = false) @ApiParam("页数") Integer currentPage,
            @RequestParam(required = false) @ApiParam("条数") Integer pageSize
    ) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        if (startDate != null) {
            startTime = startDate.atStartOfDay();
        }
        if (endDate != null) {
            endTime = endDate.atTime(23, 59, 59);
        }
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);
        return refundService.searchRefund(customerName, customerPhone, startTime, endTime, pageHelper);
    }

    @PostMapping("/update")
    @ApiOperation("待退款中上传图片")
    public Result addRefundPic(@RequestBody @ApiParam(value = "待退款dto，包含id、图片、回执单号",required = true) RefundPicDTO refundPicDTO){
            try {
                // 调用服务方法，处理pictures字段为空的情况
                return refundService.addRefundPic(refundPicDTO.getId(), refundPicDTO.getPictures(), refundPicDTO.getReceiptnumber());
            } catch (Exception e) {
                // 记录异常日志
                // 返回适当的错误响应
                return Result.error("添加退款图片失败: " + e.getMessage());
            }
    }

    @GetMapping("/images")
    @ApiOperation("图片查询")
    public Result getRefundPics(@RequestParam("id") int id) {
        return refundService.getRefundPics(id);
    }

    @GetMapping("/select")
    @ApiOperation("退款完成全查询")
    public   Result<Map<String, Object>> allRefund(@RequestParam(required = false) @ApiParam("页数") Integer currentPage,
                                              @RequestParam(required = false) @ApiParam("条数") Integer pageSize) {
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);
        return refundService.selectRefund(null,null,null,null,pageHelper);

    }


    @GetMapping("/allRefund")
    @ApiOperation("退款完成条件查询")
    public   Result<Map<String, Object>> selectRefund(
            @RequestParam(required = false) @ApiParam("客户姓名") String customerName,
            @RequestParam(required = false) @ApiParam("客户电话") String customerPhone,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @ApiParam("开始日期") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @ApiParam("结束日期") LocalDate endDate,
            @RequestParam(required = false) @ApiParam("页数") Integer currentPage,
            @RequestParam(required = false) @ApiParam("条数") Integer pageSize
    ) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        if (startDate != null) {
            startTime = startDate.atStartOfDay();
        }
        if (endDate != null) {
            endTime = endDate.atTime(23, 59, 59);
        }
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);
        return refundService.selectRefund(customerName, customerPhone, startTime, endTime, pageHelper);
    }
}
